import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home },
]

const router = new VueRouter({
  routes
})

// // to代表将要访问的页面路径
// from代表从哪个页面路径过来next代表放行的函数 
router.beforeEach((to, from, next) => {
  // 如果用户访问的是登录页，直接放行
  if (to.path === '/login') return next();
  //  从sessionStorage中 获取到保存的token的值
  const tokenstr = window.sessionStorage.getItem('token');
  //  没有token，强制跳转到登录页
  if (!tokenstr) return next('/login');
  next()
})
export default router
